package io.zhengsh.juc._3queue;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class BlockingDequeTest {

    public static void main(String[] args) {
        BlockingDeque<String> blockingDeque = new LinkedBlockingDeque<>(1);
        // offer,poll 线程安全/阻塞 api
        blockingDeque.offer("添加第一个元素");
        String item = blockingDeque.poll();
        System.out.println("poll item:" + item);

        // offer,poll 线程安全/如果失败抛出异常
        try {
            blockingDeque.put("添加第二个元素");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            String take = blockingDeque.take();
            System.out.println("take item:" + take);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        // add,remove 不是线程安全的
        blockingDeque.add("添加第四个元素");
        blockingDeque.add("添加第五个元素");
        item = blockingDeque.remove();
        System.out.println(item);
    }
}
